package com.kpx.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.kpx.app.R;
import com.kpx.app.activity.ActivityUploadManager;
import com.kpx.app.base.ComAdapter;
import com.kpx.app.base.ComViewHolder;
import com.kpx.app.bean.UploadBean;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.configs.Constants;
import com.kpx.app.utils.uiutils.ToastUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/5.
 */

public class UploadAdapter extends ComAdapter {

    /**
     * 构造方法
     *
     * @param context  上下文对象
     * @param datas    数据
     * @param layoutId
     * @version 1.0
     * @createTime 2015年5月24日, 上午11:47:30
     * @updateTime 2015年5月24日, 上午11:47:30
     * @createAuthor chencong
     * @updateAuthor chencong
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    public UploadAdapter(Context context, List datas, int layoutId) {
        super(context, datas, layoutId);
    }

    @Override
    public void convert(ComViewHolder holder, Object o, final int position) {
        final UploadBean b = (UploadBean) o;
        String status = "";
        String status2 = "";
        holder.setTextColor(R.id.txt_status,R.color.grey_80);
        switch (b.getStatus()) {
            case UploadBean.UPLOAD_TYPE_WAITTING://等待上传中
                status = "等待上传中";
                status2 = "等待上传中";
                break;
            case UploadBean.UPLOAD_TYPE_SUCCESSED://上传成功
                status = "上传成功";
                status2 = "点击绑定";
                holder.setTextColor(R.id.txt_status,R.color.red_FF4088);
                break;
            case UploadBean.UPLOAD_TYPE_FAIL://上传失败
                status = "上传失败";
                status2 = "上传失败";
                break;
            case UploadBean.UPLOAD_TYPE_STOP://暂停上传
                status = "暂停上传";
                status2 = "暂停";
                break;
            case UploadBean.UPLOAD_TYPE_BIND_FAIL://绑定失败
                status = "绑定失败";
                status2 = "点击绑定";
                break;
            case UploadBean.UPLOAD_TYPE_BIND_FAIL_2://绑定失败
                status = "绑定失败";
                status2 = "点击取消";
                break;
            case UploadBean.UPLOAD_TYPE_BIND_SUCCESS://绑定的订单成功
                status = "绑定成功";
                status2 = "绑定成功";
                holder.setTextColor(R.id.txt_status,R.color.red_FF4088);
                break;
            case UploadBean.UPLOAD_TYPE_UPLOADING://上传中
                status = "上传中"+b.getUploaProgress() + "%";
                status2 = b.getUploaProgress() + "%";
                break;
            case UploadBean.UPLOAD_TYPE_BIND_ING://绑定中
                status = "绑定中";
                status2 = "绑定中";
                break;
        }
        holder.setText(R.id.txt_statusshow, status2)
                .setText(R.id.txt_status, TextUtils.isEmpty(b.getFailMsg()) ? status : b.getFailMsg())
                .setImageLocal(R.id.img_upload, b.getLocalPath())
                .setText(R.id.txt_name,  b.getTitleName())
                .setProgress(R.id.progressBar, b.getUploaProgress())
                .setVisibility(R.id.progressBar, b.getUploaProgress() == 100 ? ComViewHolder.GONE : ComViewHolder.VISIBLE);
        holder.getView(R.id.img_upload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                switch (b.getStatus()) {
                    case UploadBean.UPLOAD_TYPE_SUCCESSED://上传成功--->提示进行绑定中
                        intent.setAction(BroadcastFilters.BROADCAST_ACTION_UPLOAD_STATUS_REBIND);
                        intent.putExtra(Constants.KEY_UPLOAD_BEAN, b);
                        mContext.sendBroadcast(intent);
                        break;
                    case UploadBean.UPLOAD_TYPE_FAIL://上传失败--->重新上传
                    case UploadBean.UPLOAD_TYPE_WAITTING://等待上传中--->开始上传
                    case UploadBean.UPLOAD_TYPE_STOP://暂停上传-->开始上传
                        intent.setAction(BroadcastFilters.BROADCAST_ACTION_UPLOAD_STATUS_BEGIN);
                        intent.putExtra(Constants.KEY_UPLOAD_BEAN, b);
                        mContext.sendBroadcast(intent);
                        break;
                    case UploadBean.UPLOAD_TYPE_BIND_FAIL://绑定失败--重新绑定
                        intent.setAction(BroadcastFilters.BROADCAST_ACTION_UPLOAD_STATUS_REBIND);
                        intent.putExtra(Constants.KEY_UPLOAD_BEAN, b);
                        mContext.sendBroadcast(intent);
                        break;
                    case UploadBean.UPLOAD_TYPE_BIND_FAIL_2://绑定失败--点击取消
                        ((ActivityUploadManager)mContext).deleteTask((UploadBean) mDatas.get(position));
                        break;
                    case UploadBean.UPLOAD_TYPE_BIND_SUCCESS://绑定的订单成功-->上传成功
                        break;
                    case UploadBean.UPLOAD_TYPE_UPLOADING://上传中-->暂停
                        intent.setAction(BroadcastFilters.BROADCAST_ACTION_UPLOAD_STATUS_CANCEL);
                        intent.putExtra(Constants.KEY_UPLOAD_BEAN, b);
                        mContext.sendBroadcast(intent);
                        break;
                    case UploadBean.UPLOAD_TYPE_BIND_ING://绑定中-->提示绑定中
                        if (b.getUploadType() == UploadBean.TYPE_ORDER_IMG) {
                        } else if (b.getUploadType() == UploadBean.TYPE_PHOTO_IMG) {
                        }
                        break;
                }
            }
        });
    }
}
